package de.jmda.gen;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.apache.commons.lang.StringUtils;

public abstract class GeneratorValidation
{
	private final static Validator VALIDATOR =
			Validation.buildDefaultValidatorFactory().getValidator();

	public static void validate(Generator generator) throws GeneratorException
	{
		Set<ConstraintViolation<Generator>> constraintViolations =
		    VALIDATOR.validate(generator);

		List<String> messages = new ArrayList<String>();

		for (ConstraintViolation<Generator> constraintViolation :
		     constraintViolations)
		{
			messages.add(
					  constraintViolation.getRootBeanClass().getName() + ": "
			    + constraintViolation.getMessage());
		}

		if (messages.size() > 0)
		{
			throw new GeneratorException(
					StringUtils.join(messages.toArray(), System.lineSeparator()));
		}
	}
}